using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Admin.IdentifyWinners
{
    public partial class frmMain : Form
    {
        const string appTitle = "Auction System";
        int _hour, _min;

        private static void About()
        {
            string msg = "TMDT-06HCA-39, ALLIES";
            MessageBox.Show(msg, appTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ControlStatus(bool state)
        {
            btnIdentify.Enabled = state;
            txtHour.ReadOnly = state;
            txtMinute.ReadOnly = state;
            btnStart.Enabled = !state;
            if (state) btnStop.Enabled = !state;
        }

        private void TimerControlsStatus(bool running)
        {
            btnStart.Enabled = !running;
            btnStop.Enabled = running;
            txtHour.ReadOnly = running;
            txtMinute.ReadOnly = running;
        }

        private static void CallWS()
        {
            try
            {
                AuctionSystemWS.WebService proxy = new AuctionSystemWS.WebService();
                proxy.PreAuthenticate = true;
                proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                proxy.IdentifyWinners();
                MessageBox.Show("Done", appTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, appTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            SystrayHelper.MinimizeToSystray(this, sysTrayManager);
        }

        private void sysTrayManager_DoubleClick(object sender, EventArgs e)
        {
            SystrayHelper.ShowWindow(this);
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            SystrayHelper.ShowWindow(this);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            About();
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            About();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIdentify_Click(object sender, EventArgs e)
        {
            Cursor c = Cursor.Current;
            this.Cursor = Cursors.WaitCursor;
            CallWS();
            this.Cursor = c;
        }

        private void rbAutomatically_Click(object sender, EventArgs e)
        {
            ControlStatus(false);
        }

        private void rbManually_Click(object sender, EventArgs e)
        {
            timer.Enabled = false;
            ControlStatus(true);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            _hour = Convert.ToInt32(txtHour.Value);
            _min = Convert.ToInt32(txtMinute.Value);
            TimerControlsStatus(true);
            timer.Enabled = true;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            int h = DateTime.Now.Hour;
            int m = DateTime.Now.Minute;
            if (h == _hour && m == _min) CallWS();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            TimerControlsStatus(false);
        }

        private void mnuIdentify_Click(object sender, EventArgs e)
        {
            CallWS();
        }
    }
}